package eu.codlab.androidemu.utilities;

import java.io.File;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import eu.codlab.androidemu.gbcdroid.EmulatorActivity;
import eu.codlab.game.gameba.activities.GameBoidActivity;
import eu.codlab.game.gameba.activities.GameBoidFragmentActivity;
import eu.codlab.game.gameba.activities.GameBoidFragmentActivityTablet;
import eu.codlab.src.game.R;

public class GameLoader {
	private static final int max_slot=10;

	public static String getGameMainImage(String game){
		Log.d("game",game);
		if(game.lastIndexOf(".") > 0)
			game = game.substring(0, game.lastIndexOf("."));
		for(int i = 0; i<max_slot;i++)
			if(new File(game+".ss"+i+".png").exists())
				return game+".ss"+i+".png";
		return null;
	}

	public static void askZip(final GameLoaderListener listener, final Context context, final boolean bigscreen, final Uri uri){
		ZipRead zipread = new ZipRead();
		zipread.compute(new File(uri.getPath()));
		if(zipread.isGBA() && false == zipread.isGBC()){
			loadGBA(listener, context, bigscreen, uri);
		}else if(false == zipread.isGBA() && zipread.isGBC()){
			loadGBC(listener, context, bigscreen, uri);
		}else{
			//TODO manage wrong file?
			DialogInterface.OnClickListener l =
					new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case DialogInterface.BUTTON_POSITIVE:
						loadGBA(listener, context, bigscreen, uri);
						break;
					case DialogInterface.BUTTON_NEGATIVE:
						loadGBC(listener, context, bigscreen, uri);
						break;
					}
				}
			};

			new AlertDialog.Builder(context).
			setCancelable(false).
			setTitle(R.string.gba_gbc_ask_title).
			setMessage(R.string.gba_gbc_ask_message).
			setPositiveButton(R.string.gba, l).
			setNegativeButton(R.string.gbc, l).
			show();
		}
	}

	public static void askBin(final GameLoaderListener listener, final Context context, final boolean bigscreen, final Uri uri){

		DialogInterface.OnClickListener l =
				new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case DialogInterface.BUTTON_POSITIVE:
					loadGBA(listener, context, bigscreen, uri);
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					dialog.dismiss();
					break;
				}
			}
		};

		new AlertDialog.Builder(context).
		setCancelable(false).
		setTitle(R.string.gba_bin_ask_title).
		setMessage(R.string.gba_bin_ask_message).
		setPositiveButton(R.string.sure_bin, l).
		setNegativeButton(R.string._return, l).
		show();
	}

	public static void loadGBA(GameLoaderListener listener, Context context, boolean bigscreen, Uri uri){
		Intent intent=null;
		context.getSharedPreferences("files", 0).edit().putString("file", uri.getPath()).commit();

		if(bigscreen){
			intent = new Intent(Intent.ACTION_VIEW, uri,
					context, GameBoidFragmentActivityTablet.class);
		}else{
			/* must not be no longer useful */
			if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2){
				intent = new Intent(Intent.ACTION_VIEW, uri,
						context, GameBoidActivity.class);
			}else{
				intent = new Intent(Intent.ACTION_VIEW, uri,
						context, GameBoidFragmentActivity.class);
			}
		}

		if(listener.isConsole()){
			intent.putExtra("console", true);
		}

		listener.onGameLoaderIntent(intent);
	}

	public static void loadGBC(GameLoaderListener listener, Context context, boolean bigscreen, Uri uri){

		Intent intent = new Intent(Intent.ACTION_VIEW, uri,
				context, EmulatorActivity.class);
		listener.onGameLoaderIntent(intent);
	}
}
